angular.module('Controller')
.controller('QgyScoreGoodEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    $scope.type=type=items.type
    $scope.data=items.data
    $scope.modalTitle=if type=='new' then '新建积分产品' else '编辑积分产品'
    $scope.modalEnableList=commonConstant.enabledList()
    $scope.qgyGoodStatusList=commonConstant.qgyGoodStatusList()
    $scope.modalCityList=commonConstant.qgyCityList()
    if $scope.data.city==undefined and type=='new' and $scope.modalCityList?.length>0
        $scope.data.city= $scope.modalCityList[0].value
    _.defer(()->
        $('#photo-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif','jpeg'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                saveUrl = data.response.message.saveUrl+""
                if(saveUrl.indexOf("http")==0)
                    $scope.data.thumb = saveUrl
                else
                    $scope.data.thumb=urls.backend+'/public/'+$scope.result['saveUrl']

                $scope.showPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
    )
    _.extend $scope,
        newData:()->
            reqData={
                "title":  $scope.data.title
                "type":  $scope.data.type
                "description":$scope.data.description
                "content":$scope.data.content
                "thumb":$scope.data.thumb
                "city":$scope.data.city
                "salesPrice":0
                "displayOrder":$scope.data.displayOrder
                "enabled":$scope.data.enabled
                "score":$scope.data.score
            }
            $http.post('/goods',reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
            reqData={
                "title":  $scope.data.title
                "description":$scope.data.description
                "content":$scope.data.content
                "thumb":$scope.data.thumb
                "city":$scope.data.city
                "salesPrice":$scope.data.salesPrice
                "score":$scope.data.score
                "displayOrder":$scope.data.displayOrder
                "enabled":$scope.data.enabled
            }
            $http.put("/goods/#{$scope.data.id}",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)